<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gantipass extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		
		$index	= $this->config->item('index_page');
		$host	= $this->config->item('base_url');

		$this->url = empty($index) ? $host : $host . $index . '/';
		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
		$this->load->model('mGantiPass');
		
		//get session.. 'user' tuh nama session yang udah di set 
//		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
	}
	
	public function index(){
		if (!empty($this->user)){

		$param['url'] = $this->url;
		$view['header'] = $this->load->view('admin/header/header', $param, true);
		$view['menu'] = $this->load->view('admin/menu/menu', $param, true);
		$view['content'] = $this->load->view('admin/rcontent/gantipass/gantipass', $param, true);
		$this->load->view('index', $view);
		}
		else
	   {
		 //If no session, redirect to login page
		 redirect('admin/login');
	   }
	}
	
	function get_pass(){
	
		//set validation properties
		$this->_set_rules();
		
		//run validation
		if($this->form_validation->run()===FALSE){
			$data['message']='form harus diisi';
		}else {
			//save data
			$pass=array('pass_lama'=>$this->input->post('pass_lama'),'pass_baru'=>$this->input->post('pass_baru'),'pass_ulangi'=>$this->input->post('pass_ulangi'));
			if($pass['pass_baru']==$pass['pass_ulangi']){
			$query = $this->mGantiPass->get_by_pass($pass['pass_lama']);
				if($query->num_rows()>0)
				{
					foreach($query->result_array() as $row)
					{
						$this->mGantiPass->update($row['ADMINNAME'],$pass['pass_baru']);
						//set user message
						$data['message']='ganti password berhasil';
					}
				}else 
				{
					$data['message']='password lama salah';
				}
			} else
			{
				$data['message']='password baru dan password ulangi tidak sama';
			}
		}
		$data['link_back']=anchor('admin/home/','Kembali ke home',array('class'=>'back'));
		//load view
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/gantipass/gantipass', $data, true);
		$this->load->view('index',$data);
		
		
	}

	function _set_rules(){
		$this->form_validation->set_rules('pass_lama','pass_lama','required|trim');
		$this->form_validation->set_rules('pass_baru','pass_baru','required|trim');
		$this->form_validation->set_rules('pass_ulangi','pass_ulangi','required|trim');
	}

}